package com.ucar.ui.splash;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.text.TextUtils;

import com.ucar.R;
import com.ucar.net.BaseResponse;
import com.ucar.net.Callback;
import com.ucar.net.NetRequest;
import com.ucar.ui.base.BaseActivity;
import com.ucar.ui.main.MainActivity;

/**
 * 启动页
 * @author moon
 *
 */
public class SplashActivity extends BaseActivity 
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView( R.layout.activity_splash );
		init();
	}
	
	/**
	 * 初始化
	 */
	private void init()
	{
		final String token = mSpUtil.getToken();
		if ( TextUtils.isEmpty( token ) )
		{
			this.getWindow().getDecorView().postDelayed(new Runnable() {
				@Override
				public void run() 
				{
					redirectActivity( MainActivity.class );
					finish();
				}
			}, 3000);
		}
		else
		{
			this.getWindow().getDecorView().postDelayed(new Runnable() {
				@Override
				public void run() 
				{
					reqCheckToken( token );
				}
			}, 1500);
			
		}
	}
	
	@Override
	public void onInit() {}

	@Override
	public void onFindViews() {}

	@Override
	public void onInitViewData() {}

	@Override
	public void onBindListener() {}
	
	/**
	 * 请求检查token
	 */
	private void reqCheckToken( final String token ) 
	{
		// Tag used to cancel the request
		String tag = "tag_request_checktoken";
		NetRequest.getInstance(this).get(new Callback() {

			@Override
			public void onSuccess(BaseResponse response) 
			{
				redirectActivity( MainActivity.class );
				finish();
			}

			@Override
			public void onPreExecute() {}

			@Override
			public void onFinished() {}

			@Override
			public void onFailed(String msg) 
			{
				mSpUtil.setLogin( false );
				redirectActivity( MainActivity.class );
//				showToast( msg );
				finish();
			}

			@Override
			public Map<String, String> getParams() {
				Map<String, String> params = new HashMap<String, String>();
				params.put("url", "app/tokencheck");
				params.put("token", token );
				return params;
			}
		}, tag);
	}

}
